const moment = require('../libs/moment.min.js');
const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
let _lastTime = null
function throttle(fn, gapTime) {
  if (gapTime == null || gapTime == undefined) {
    gapTime = 1500
  }

  //let _lastTime = null
  return function (e) {
    let _nowTime = + new Date()
    if (_nowTime - _lastTime > gapTime || !_lastTime) {
      fn(e)
      _lastTime = _nowTime
    }
  }
}
/**
 * 计算缴纳保证金的比例
 */
var calcRate = function (score) {
  var rate = 0;
  if (score >= 0 && score < 66) {
    if (score >= 0 && score < 12) {
      rate = 0.20;
    } else if (score >= 12 && score < 18) {
      rate = 0.19;
    } else if (score >= 18 && score < 24) {
      rate = 0.18;
    } else if (score >= 24 && score < 30) {
      rate = 0.17;
    } else if (score >= 30 && score < 66) {
      rate = 0.16;
    }
  } else if (score >= 66 && score < 390) {
    if (score < 102) {
      rate = 0.15;
    } else if (score >= 102 && score < 138) {
      rate = 0.14;
    } else if (score >= 138 && score < 174) {
      rate = 0.13;
    } else if (score >= 174 && score < 390) {
      rate = 0.12;
    }
  } else if (score >= 390) {
    if (score < 606) {
      rate = 0.11;
    } else {
      rate = 0.10;
    }
  }
  return rate;
}
module.exports = {
  formatTime: formatTime,
  throttle: throttle,
  calcRate: calcRate,
  moment: moment
}
